<?php
declare (strict_types = 1);
namespace app\promotion\model;

use app\common\model\BaseModel;
use app\user\logic\user\Account;
use think\model\concern\SoftDelete;

/**
 * 团购活动模型
 */
class PromotionTuanData extends BaseModel
{
    use softDelete;
    /**
     * 表名
     * @var string
     */
    protected $name = 'promotion_tuan_data';

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'            => 'integer',
        'tuan_id'       => 'integer',
        'user_id'       => 'integer',
        'create_time'   => 'integer',
        'status'        => 'integer',
    ];

    /**
     * 定义表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 入团状态查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-14
     * @param  [type]  $query  [description]
     * @param  integer $status [description]
     * @return [type]          [description]
     */
    public function scopeStatus($query, $status = 1)
    {
        $query->whereStatus($status);
    }

    /**
     * 活动条件查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-08
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopeUserId($query, $user_id = 0)
    {
        $user_id = $user_id ? : request()->mid;
        $query->whereUserId($user_id);
    }

    /**
     * 关联团基础信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-15
     * @return [type] [description]
     */
    public function tuan()
    {
        return $this->belongsTo(PromotionTuan::class, 'tuan_id');
    }

    /**
     * 用户头像名称关联
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-09
     * @param string $value [description]
     */
    public function userAvatarName()
    {
        return $this->belongsTo(Account::class, 'user_id')->with(['avatar_url'])->bind(['current_user_name' => 'nick_name', 'current_user_avatar_url' => 'avatar_url']);
    }
}
